﻿ var clock = '';
 var nums = 60;
 var btn;

 window.onload = function() {
	
     
        // alert('服务器正在维护,请稍后再试')
     var obj = JSON.parse(window.localStorage.getItem('obj'));
     var data =  {
        storeId:obj.storeId,
        loginFlag:'boss'
     }
    // var data =  {
    //     storeId:'10011',
    //     loginFlag:'boss'
    //  }
     var provinces = {};

    $.ajax({
         type: 'post',
         url: url + 'shipment/order/get/cart',
         data:{
            storeId: data.storeId,
            loginFlag:data.loginFlag
         },
         success: function(res) {
             if (res.code == 200) {
                 var res = res.datas;
                 var html = template("list", res);
                 $('.content').html(html);
                 $('#sanji').initLoad({
                     proId: res.storeInfo.provinceId,
                     cityId: res.storeInfo.cityId,
                     areaId: res.storeInfo.areaId,
                 }, function(obj) {
                     provinces = obj;
                 });
             } else if (res.code == 400) {
		            alert(res.msg)
                 //toast.hide();
                 //showMsg("text", res.msg);


             }

         }
     })




     //  二次弹窗提示
     $('.footer').click(function() {
         var arr = {
             listCart: []
         };
         if ($('.msg').val() == '') {
             showErr('code')
         } else {
             $("#bg").show();
             $('.dialog').fadeIn();
             $('.userName').html($('.name').val());
             $('.telphone').html($('.telephone').val());
             $('.address').html(provinces.pro[1] + provinces.city[1] + provinces.area[1] + $('.add').val())
             var idArr = $(".goodsList").find('.boutiqueName');
             var numArr = $(".goodsList").find('.num');
             for (var i = 0; i < idArr.length; i++) {
                 for (var j = 0; j < numArr.length; j++) {
                     if (i == j) {
                         arr.listCart.push({
                             bouName: idArr[i].innerHTML,
                             quantity: numArr[j].innerHTML,
                             index: i + 1
                         })
                     }
                 }
             }
             var html = template('goods', arr);
             $(".goods-li").html(html);


         }
     })
     $("#bg").click(function() {
         $(this).hide();
         $(".dialog").hide();
     })
     $('.dialog-button').click(function() {
         //获取传参数据
         var arr = {
             storeId: data.storeId,
             takeDeliveryName: $('.name').val(),
             takeDeliveryMobile: $('.telephone').val(),
             address: provinces.pro[1] + provinces.city[1] + provinces.area[1],
             addressDetails: $('.add').val(),
             provinceId: provinces.pro[0],
             cityId: provinces.city[0],
             areaId: provinces.area[0],
             buyRemark: $('.remak').val(),
             smsAuthCode: $('.msg').val(),
             listCart: []
         };

         //循环得到精品ID和对应的数量
         var idArr = $(".goodsList").find('.boutiqueId');
         var numArr = $(".goodsList").find('.num');
         for (var i = 0; i < idArr.length; i++) {
             for (var j = 0; j < numArr.length; j++) {
                 if (i == j) {
                     arr.listCart.push({
                         bouId: idArr[i].innerHTML,
                         quantity: numArr[j].innerHTML
                     })
                 }
             }
         }

         //请求接口
         $.ajax({
             type: "post",
             url: url + 'shipment/order/save/info',
             data: JSON.stringify(arr),
             contentType: 'application/json',
             success: function(res) {
                 if (res.code == 200) {
                     openNewPage('boutique_list.html');
                 } else if (res.code == 400) {
                     alert(res.msg)
                 }
             }
         })
     })

 }

 //去订单记录页面
 function openNewPage(url) {
     if (isAndroid()) {
         window.WebViewJavascriptBridge.callHandler(
             'openNewPage', {
                 'param': url
             },
             function(responseData) {}
         );
     } else if (isIOS()) {
         window.location.href = url
     } else {
         window.location.href = url
     }
 }



 //发送验证码
 function sendCode(thisBtn) {
     var telephone = $(".telephone").val();
     $.ajax({
         type: "get",
         url: url + "storeboss/getsms/" + telephone,
         success: function(res) {
             if (res.code == 200) {
                // showDefaultMsg('success'); //如果请求成功，显示发送验证码成功
                 btn = thisBtn;
                 btn.disabled = true; //将按钮置为不可点击
                 btn.innerHTML = nums + '秒';
                 clock = setInterval(doLoop, 1000); //一秒执行一次
                 $('.phone').html(telephone)
                 $('.text').show();
             }
         },
         error: function(err) {

         }
     });


 }

 // 定时器倒计时
 function doLoop() {
     nums--;
     if (nums > 0) {
         btn.innerHTML = nums + '秒';
     } else {
         clearInterval(clock); //清除js定时器
         btn.disabled = false;
         btn.innerHTML = '重新发送';
         nums = 60; //重置时间
     }
 }



 //提示发送成功和失败
 var toast = new auiToast({});

 function showDefaultMsg(type) {
     switch (type) {
         case "success":
             toast.success({
                 title: "短信发送成功",
                 duration: 1500
             });
             break;
         case "fail":
             toast.fail({
                 title: "短信发送失败",
                 duration: 1500
             });
             break;
         case "loading":
             toast.loading({
                 title: "加载中",
                 duration: 1500
             });
             break;
     }
 }

 function showErr(type) {
     switch (type) {
         case "error":
             toast.fail({
                 title: "请输入备注信息",
                 duration: 1500
             });
             break;
         case "code":
             toast.fail({
                 title: "请输入验证码",
                 duration: 1500
             });
             break;
     }
 }

 function showMsg(type, text) {
     switch (type) {
         case "text":
             toast.fail({
                 title: text,
                 duration: 2000
             });
             break;
     }
 }